const app = getApp()
const _util = require('../../utils/util.js')
const Api = require('../../api/api.js')
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    inputShowed: false,
    inputVal: "",
    navlist: [
      {
        title: '全部',
        type: ''

      }, {
        title: '待支付',
        type: 'wait_pay'
      },
      {
        title: '已付款',
        type: 'payed'
      },
      {
        title: '已退款',
        type: 'refunded'
      }
    ],
    sort: '',
    currentIndex: 0,
    orderList: [{}, {}],
    transStatus: {},
    items: []
  },

  onLoad: function () {
    console.log(' myorder index onLoad')
    var that = this;
    wx.createSelectorQuery().select('#slideBar').boundingClientRect(function (rect) {
      rect.width   // 节点的宽度
    }).exec(function (res) {
      sliderWidth = res[0].width;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            sliderLeft: (res.windowWidth / that.data.navlist.length - sliderWidth) / 2,
            sliderOffset: res.windowWidth / that.data.navlist.length * that.data.activeIndex
          });
        }
      });
    })
    this.bindData()
  },
  tabClick: function (e) {
    var sorttype = e.currentTarget.dataset.sorttype;
    this.setData({
      sort: sorttype,
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    })
    this.bindData()
  },

  // 取消订单
  cancelClick: function (event) {
    var pagethis = this, index = Number(event.target.dataset.index)
    wx.showModal({
      title: '订单取消',
      content: '确定取消该订单？',
      success: function (rps) {
        if (rps.confirm) {
          Api.cancelOrder({
            data:app.addParams({
              orderId: event.target.dataset.id,
            }),
            success: (res) => {
              let _items = pagethis.data.items;
              _items.splice(index, 1)
              pagethis.setData({
                items: _items
              })
            }
          })        
        }

      }
    })

  },
  // 支付
  payClick: function (event) {
    console.log("to orderpay rps:" + event.target.dataset.id);
    var pagethis = this
    Api.toWxPay({
      data:app.addParams({
        orderId: event.target.dataset.id
      }),
      success:(rps) => {
        wx.requestPayment({
          'timeStamp': rps.data.data.timeStamp,
          'nonceStr': rps.data.data.nonceStr,
          'package': rps.data.data.packageStr,
          'signType': 'MD5',
          'paySign': rps.data.data.paySign,
          'success': function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
           
            pagethis.bindData()
          },
          'fail': function (rqs) {
            wx.showModal({
              title: '支付失败',
              content: rqs.data.msg,
              success: function (rqs) {

              }
            })
          }
        })
      }
    })
  },
  // 退单
  refundClick: function (event) {
    var pagethis = this
    wx.showModal({
      title: '退单',
      content: '确定退单？',
      success: function (rps) {
        if (rps.confirm) {
          Api.refundOrder({
            data:app.addParams({
              orderId: event.target.dataset.id
            }),
            success:(res) => {
               wx.showToast({
                title: '已完成',
                icon: 'success',
                duration: 2000
              });
            
              pagethis.bindData();
            }
          })
        }

      }
    })
  },
  // 数据绑定
  bindData: function (e) {
    
    var pagethis = this
    Api.getOrderList({
      data:app.addParams({
        start: '0',
        count: '1000',
        payStatus: this.data.sort
      }),
      success:(res) => {
        console.log('+++++++++++++++++++++++订单列表++++++++++++++++++++++++++')
        console.log(res)
        let _items = res.data.data.orderPage.items;
        for (let i = 0, len = _items.length; i < len; i++) {
          _items[i].payStatusName = _util.getOrderStatus(_items[i].payStatus);
          _items[i].createTime = _util.formatTime(new Date(_items[i].createTime))
        }
        pagethis.setData({
          items: _items
        })
      },
      fail: function (e) {
        wx.hideToast()
        wx.showModal({
          title: '请求错误',
          content: '请稍后刷新',
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    })
  }

})